//
//  ModifyPasswordViewController.m
//  EMMApp
//
//  Created by Mac on 16/5/3.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "ModifyPasswordViewController.h"

/** Api
 *
 *  首页Api调用导入.h文件
 */
#import "EditPwdApi.h"

@interface ModifyPasswordViewController ()<UITextFieldDelegate>

@end

@implementation ModifyPasswordViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationController];
    self.view.backgroundColor = [UIColor colorFromHexCode:@"#f7f7f7"];
    
    _oldPassword.delegate = self;
    _oldPassword.placeholder = @"请输入旧密码";
    _oldPassword.font = [UIFont systemFontOfSize:14];
    _oldPassword.returnKeyType =UIReturnKeyDone;
    _oldPassword.secureTextEntry = YES;
    [_oldPassword addTarget:self action:@selector(oldPasswordDidChange:) forControlEvents:UIControlEventEditingChanged];

    _password.delegate = self;
    _password.placeholder = @"请输入新密码";
    _password.font = [UIFont systemFontOfSize:14];
    _password.returnKeyType =UIReturnKeyDone;
    _password.secureTextEntry = YES;
    [_password addTarget:self action:@selector(passwordDidChange:) forControlEvents:UIControlEventEditingChanged];

    _confirmPassword.delegate = self;
    _confirmPassword.placeholder = @"请再次输入新密码";
    _confirmPassword.font = [UIFont systemFontOfSize:14];
    _confirmPassword.returnKeyType =UIReturnKeyDone;
    _confirmPassword.secureTextEntry = YES;
    [_confirmPassword addTarget:self action:@selector(confirmPasswordDidChange:) forControlEvents:UIControlEventEditingChanged];

    _preservationBtn.layer.masksToBounds = YES;
    _preservationBtn.layer.cornerRadius = 5;
    _preservationBtn.backgroundColor = [UIColor colorFromHexCode:@"#e97300"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"修改密码";
    [self configLeftBtn];
}

- (void)oldPasswordDidChange:(UITextField *)textField {
    if (textField == self.oldPassword) {
        if (textField.text.length > 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

- (void)passwordDidChange:(UITextField *)textField {
    if (textField == self.password) {
        if (textField.text.length > 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

- (void)confirmPasswordDidChange:(UITextField *)textField {
    if (textField == self.confirmPassword) {
        if (textField.text.length > 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

#pragma amrk - UIbutton - 保存 - 调用的方法
- (IBAction)preservationBtnClick:(id)sender {
    if (_oldPassword.text.length == 0) {
        [self showToast:@"旧密码不能为空"];
        return;
    }
    
    if (_oldPassword.text.length < 6) {
        [self showToast:@"旧密码不能小于6位"];
        return;
    }
    
    if (_password.text.length == 0) {
        [self showToast:@"新密码不能为空"];
        return;
    }
    
    if (_password.text.length < 6) {
        [self showToast:@"新密码不能小于6位"];
        return;
    }
    
    if (_confirmPassword.text.length == 0) {
        [self showToast:@"确认新密码不能为空"];
        return;
    }
    
    if (_confirmPassword.text.length < 6) {
        [self showToast:@"确认新密码不能小于6位"];
        return;
    }
    
    if (![_password.text isEqualToString:_confirmPassword.text]) {
        [self showToast:@"新密码与确认新密码不一致"];
        return;
    }
    
    [self creatUpExitOldPassword:_oldPassword.text newPwd:_confirmPassword.text];
}

#pragma mark - 修改密码-EditPwdApi
- (void)creatUpExitOldPassword:(NSString *)oldPassword newPwd:(NSString *)newPwd  {
    [self showHudInWindow:@"正在加载请稍等..."];
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *merchantId = [userdf objectForKey:@"merchantId"];
    NSString *userId = [userdf objectForKey:@"userNameId"];
    NSString *token = [userdf objectForKey:@"is_login"];
    EditPwdApi *api = [[EditPwdApi alloc] initWithMerchantId:merchantId userId:userId token:token loginPwd:oldPassword newPwd:newPwd];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        EditPwdApi *result = (EditPwdApi *)request;
        if (result.isSuccess) {
            NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
            NSDictionary *dictionary = [defs dictionaryRepresentation];
            for(NSString *key in [dictionary allKeys]){
                if ([key isEqualToString:@"DeviceToken123"] ||
                    [key isEqualToString:@"loginText"] ||
                    [key isEqualToString:@"provinceCityDistrict"]) {
                    
                }else {
                    [defs removeObjectForKey:key];
                    [defs synchronize];
                }
            }
            [appInstance showLogin];
        } else {
            [self showToast:result.resultData.errorMsg];
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

@end
